const {
    Mysql,
    validateParams,
    TripleEncrypt
} = require('../tools');
module.exports = async ctx => {
    let {
        uid
    } = ctx.query;
    let {
        vid,
        type
    } = ctx.request.body;
    let isTrue = validateParams({
        vid,
        uid,
        type
    });
    if (!isTrue) {
        ctx.state.data = {
            code: '002',
            err: 'مەغلۇپ بولدى'
        }
        return;
    }
    try {
        // 统计
        let aSql = `UPDATE video SET views = views+1 WHERE vid = ${Number(vid)}`;
        await Mysql(aSql);
        // 电影
        if (type == 0) {
            let sql = `SELECT * FROM video WHERE vid = ${vid}`;
            // let 
            let res = await Mysql(sql);
            if (res.length) {
                ctx.state.data = {
                    data: TripleEncrypt(JSON.stringify(res[0])),
                    code: '001'
                    // total: total[0].total,
                }
            } else {
                ctx.state.data = {
                    code: '002',
                    err: 'مەغلۇپ بولدى'
                }
            }
        } else {
            // 电视剧
            let sql = `SELECT video.isVip,video.price,video.isFree,tv.* FROM video RIGHT JOIN tv ON tv.vid=video.vid WHERE tv.vid = ${vid}`;
            let res = await Mysql(sql);
            if (res.length) {
                ctx.state.data = {
                    data: TripleEncrypt(JSON.stringify(res[0])),
                    code: '001'
                    // total: total[0].total,
                }
            } else {
                ctx.state.data = {
                    code: '002',
                    err: 'مەغلۇپ بولدى'
                }
            }
        }
    } catch (error) {
        ctx.state.data = {
            code: '002',
            err: 'مەغلۇپ بولدى'
        }
    }
}